﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using QLNHang.BUS;
using QuanLyDatVe.DataAccessLayer;

namespace QLNHang.GUI
{
    public partial class frmLogin : Form
    {
        private int phanQuyen;

        public int PhanQuyen
        {
            get { return phanQuyen; }
        }

        public frmLogin()
        {
            InitializeComponent();
            this.ControlBox = false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            String tenDangNhap = txtTenDangNhap.Text;
            MD5 md5Hash = MD5.Create();
            String matKhau = GetMd5Hash(md5Hash, txtMatKhau.Text);
            int phanQuyen = DangNhap(tenDangNhap, matKhau);
            
            this.Close();
        }

        private int DangNhap(string tenDangNhap, string matKhau)
        {
            NhanVienBUS nv = new NhanVienBUS();
            return nv.DangNhap(tenDangNhap, matKhau);
        }

        static string GetMd5Hash(MD5 md5Hash, string input)
        {
            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }
    }
}
